package search;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.Text;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.htmlparser.nodes.TextNode;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ResultFormatter {
	
	private final Logger logger = Logger.getLogger(ResultFormatter.class);
	
	public String XmlFormat(IndexSearcher searcher, TopDocs results,
			ArrayList<String> fields) throws ParserConfigurationException,
			CorruptIndexException, IOException, TransformerException {
		
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document document = builder.newDocument();
        document.setXmlVersion("1.0");
        
        Element root = document.createElement("Root");   
        document.appendChild(root);
        
        Element hitsElement = document.createElement("hits");
        root.appendChild(hitsElement);
        
        logger.info("Result count :" + results.scoreDocs.length);
		
		for(int i = 0; i < results.scoreDocs.length; ++i){
			
			logger.debug("Process " + i + "th result.");
			
			ScoreDoc scoreDoc = results.scoreDocs[i];
			org.apache.lucene.document.Document doc = searcher.doc(scoreDoc.doc);
			
	        Element hitElement = document.createElement("hit");
	        hitsElement.appendChild(hitElement);
	        
	        Element fieldsElement = document.createElement("fields");
	        hitsElement.appendChild(fieldsElement);
	        
	        for(String  field : fields){	        	
	        	String data = doc.get(field);
	        	System.out.println(data);
	  
	        	Element fieldElement = document.createElement(field);	
	        	CDATASection cdata = document.createCDATASection(data);
	        	fieldElement.appendChild(cdata);
	        	fieldsElement.appendChild(fieldElement);
	        }
		}
		
        TransformerFactory transFactory = TransformerFactory.newInstance();     
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        DOMSource domSource = new DOMSource(document);
        ByteArrayOutputStream out = new ByteArrayOutputStream();        
        StreamResult rs = new StreamResult(new PrintWriter(out));        
        transformer.transform(domSource, rs); 

		return out.toString();
	}

}
